'use strict';

exports.__esModule = true;
exports.getURLParameters = exports.coalesceFactory = exports.deepAssign = exports.deepMerge = undefined;

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; // import jsep from "./jsep";
// import evaluate from 'static-eval';

/**
 * run a expression
 *
 * @param expression
 * @param args
 */
// export function run(expression, args) {
//     let parse_tree = jsep(expression);
//     return evaluate(parse_tree, args);
// }

exports.transformStylelikeExp = transformStylelikeExp;
exports.parseObjToOptions = parseObjToOptions;
exports.parseOptionsToObj = parseOptionsToObj;
exports.sleep = sleep;
exports.chartoUnicode = chartoUnicode;
exports.param = param;

var _deepmerge = require('./deepmerge');

var _deepmerge2 = _interopRequireDefault(_deepmerge);

var _merge = require('lodash/merge');

var _merge2 = _interopRequireDefault(_merge);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var deepMerge = exports.deepMerge = _deepmerge2['default'];

/**
 *  @example
 *
 * var object = {
 *   'a': [{ 'b': 2 }, { 'd': 4 }]
 * };
 *
 * var other = {
 *   'a': [{ 'c': 3 }, { 'e': 5 }]
 * };
 *
 * _.merge(object, other);
 * // => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
 */

var deepAssign = exports.deepAssign = _merge2['default'];

function toString(type) {
    return Object.prototype.toString.call(type);
}

function isObject(obj) {
    return (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && toString(obj) === "[object Object]";
}

/**
 * 转变类似标签上css style语法
 * 
 * @param {String} str 
 * @param {String} [splitopr = ;] 
 * 
 * @example
 * 'translateX:100;' => [['translateX', '100']]
 * 
 * @return {Array} arr
 */
function transformStylelikeExp(str) {
    var splitopr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ";";

    if (!str) {
        return [];
    }
    if (str.endsWith(splitopr)) {
        str = str.slice(0, str.length - 1);
    }
    var arr = str.split(splitopr);
    arr = arr.map(function (v) {
        return v.trim().split(":").map(function (s) {
            return s.trim();
        });
    });
    return arr;
}

/**
 * 返回一个自定义合并函数，它返回从提供的参数验证函数返回true的第一个参数。使用数组。 find()返回第一个参数，它从提供的参数验证函数返回true。
 * 
 * @param {*} valid 
 */
var coalesceFactory = exports.coalesceFactory = function coalesceFactory(valid) {
    return function () {
        for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
        }

        return args.find(valid);
    };
};

var _getURLParameters = function _getURLParameters(url) {
    return (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(function (a, v) {
        return a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a;
    }, {});
};

/**
 * 获取url字符串
 * 
 * @param {*} url 
 * @param {*} isDecode 
 */
var getURLParameters = exports.getURLParameters = function getURLParameters(url) {
    var isDecode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

    var param = _getURLParameters(url);
    if (isDecode && decodeURIComponent) {
        for (var key in param) {
            param[key] = decodeURIComponent(param[key]);
        }
    }
    return param;
};

/**
 * parse obj to a option
 *
 * { a: 1 }
 *
 * [
 *     {
 *          name: "a",
 *          value: 1
 *     }
 * ]
 *
 * @param obj
 * @returns {Array}
 */
function parseObjToOptions(obj) {
    if (isObject(obj)) {
        return Object.entries(obj).map(function (v) {
            return {
                name: v[0],
                value: v[1]
            };
        });
    }
    return [];
}

/**
 * parse options to obj
 *
 * @param options
 * @returns {{}}
 */
function parseOptionsToObj(options) {
    var ret = {};
    options.forEach(function (v) {
        ret[v.name] = v.value;
    });
    return ret;
}

/**
 * js 阻塞线程
 *
 * @param delay
 */
function sleep(delay) {
    var start = new Date().getTime();
    while (new Date().getTime() - start < delay) {
        continue;
    }
}

/**
 * 转换中文到unicode
 * 
 * @param {String} s 
 */
function chartoUnicode(s) {
    return s.replace(/([\u4E00-\u9FA5])/g, function () {
        return '\\u' + RegExp["$1"].charCodeAt(0).toString(16);
    });
}

/**
 * 使用replace隐藏某段字符串部分内容
 * 
 * @deprecated
 * 
 * @param {*} str 
 * @param {*} start 
 * @param {*} end 
 * @param {*} replace 
 */
// export function setStringUnvisible(str, start, end, replace="*") { 
//     return str.slice(0, start) + replace.repeat(end - start) + str.slice(end, str.length);  
// }

/**
 * like $.param
 * 
 * @param {Object} a 
 */
function param(a) {
    return Object.keys(a).map(function (k) {
        return encodeURIComponent(k) + '=' + encodeURIComponent(a[k]);
    }).join('&');
}